package cn.xenosp.server.web.controller;

import cn.xenosp.server.common.Result;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.tools.ObjectConvert;
import cn.xenosp.server.module.menu.entity.SysMenuVO;
import cn.xenosp.server.module.menu.service.SysMenuService;
import cn.xenosp.server.module.role.entity.SysRoleVO;
import cn.xenosp.server.module.role.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 项目名： service-web
 * 包路径： cn.xenosp.server.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-12-12 09:47
 * 描述: TODO
 * 版本: 1.0
 */
@RestController
@RequestMapping("${app.config.api-base-url}/menu/")
public class MenuController {

    @Autowired
    SysMenuService menuService;

    @LogInfo(operationModuleType = "菜单模块",operationName = "分页查询",operationContent = "查询菜单数据")
    @PostMapping("/getPage")
    public Result<?> getPage(@RequestBody String body){
        try {
            SysMenuVO sysMenuVO = ObjectConvert.urlStringToClass(body, SysMenuVO.class);
            return Result.OK("",menuService.getPage(sysMenuVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "分页查询",operationContent = "查询菜单数据")
    @PostMapping("/getAllParent")
    public Result<?> getAllParent(@RequestBody String body){
        try {
            SysMenuVO sysMenuVO = ObjectConvert.urlStringToClass(body, SysMenuVO.class);
            return Result.OK("",menuService.getListById(sysMenuVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "查询下级菜单",operationContent = "查询下级菜单")
    @PostMapping("/getListById")
    public Result<?> getListById(@RequestBody SysMenuVO sysMenuVO){
        try {
            return Result.OK("",menuService.getListById(sysMenuVO));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "更新菜单",operationContent = "更新菜单")
    @PostMapping("/saveMenu")
    public Result<?> saveMenu(@RequestBody SysMenuVO sysMenuVO){
        try {
            menuService.update(sysMenuVO);
            return Result.OK("保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "更新菜单状态",operationContent = "更新菜单状态")
    @PostMapping("/status")
    public Result<?> status(@RequestBody SysMenuVO sysMenuVO){
        try {
            menuService.updateStatus(sysMenuVO);
            return Result.OK("保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("保存数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "新增菜单",operationContent = "新增菜单")
    @PostMapping("/addMenu")
    public Result<?> addMenu(@RequestBody SysMenuVO sysMenuVO){
        try {
            menuService.save(sysMenuVO);
            return Result.OK("添加成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("添加数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "删除菜单",operationContent = "删除菜单")
    @PostMapping("/delMenu")
    public Result<?> delMenu(@RequestBody SysMenuVO sysMenuVO){
        try {
            menuService.delete(sysMenuVO);
            return Result.OK("删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("删除数据失败",null);
        }
    }
    @LogInfo(operationModuleType = "菜单模块",operationName = "获取所以父级菜单",operationContent = "获取所以父级菜单")
    @PostMapping("/getAllParentResources")
    public Result<?> getAllParentResources(@RequestBody SysMenuVO sysMenuVO){
        try {
            return Result.OK("成功",menuService.getAllParentResources(sysMenuVO));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("服务错误",null);
        }
    }
}
